package com.msmall.community.klg.article.action.html;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.util.ConstantAjax;
import com.msmall.community.klg.model.ArticleReply;
import com.msmall.community.klg.service.ArticleManager;

public class ArticleReplyAction extends HTMLBaseAction{

	public static final String SESSION_ARTICLE_REPLY_KEY = "SESSION_ARTICLE_REPLY_KEY";
	
	private static final long serialVersionUID = 3721517834474862349L;
	
	private Long articleid;
	
	private ArticleReply reply;
	
	private ArticleManager articleManager;

	public void setArticleManager(ArticleManager articleManager) {
		this.articleManager = articleManager;
	}
	
	public String save(){
		try{
			boolean success = true;
			StringBuffer buffer = new StringBuffer();
			buffer.append(ConstantAjax.RETURN_PRE);
			if(!getIsLogin()){
				buffer.append(createResult("false"));
				buffer.append(createEasyContent(getText("common.authentication.anonymous")));
				success = false;
			}
			if(reply == null && success){
				buffer.append(createResult("false"));
				buffer.append(createEasyContent(getText("html.klg.reply.create.content.length.short")));
				success = false;
			}
			if(success){
				int valid = articleManager.validateReplyContent(reply.getContent());
				if(valid == 2){
					buffer.append(createResult("false"));
					buffer.append(createEasyContent(getText("html.klg.reply.create.content.length.short")));
					success = false;
				}
				if(valid == 3){
					buffer.append(createResult("false"));
					buffer.append(createEasyContent(getText("html.klg.reply.create.content.length.long")));
					success = false;
				}
				if(valid == 4){
					buffer.append(createResult("false"));
					buffer.append(createEasyContent(getText("html.klg.reply.create.content.illegal")));
					success = false;
				}
			}
			if(success){
				reply.setNickname(getNickname());
				articleManager.saveArticleReply(reply, getUserid(), getUserAgentType());
				articleid = reply.getArticleId();
				buffer.append(createResult("true"));
			}
			buffer.append(ConstantAjax.RETURN_SUFF);
			message = buffer.toString();
		}catch(Exception e){
			log.debug("============>", e);
		}
		return SUCCESS;
	}

	public ArticleReply getReply() {
		return reply;
	}

	public void setReply(ArticleReply reply) {
		this.reply = reply;
	}

	public Long getArticleid() {
		return articleid;
	}

	public void setArticleid(Long articleid) {
		this.articleid = articleid;
	}

}
